package com.danieltoms.gcSync;

import java.util.Hashtable;

import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;
import net.rim.device.api.system.RuntimeStore;
import net.rim.device.api.util.IntHashtable;

import com.danieltoms.gcSync.util.CryptoUtil;

public class GcSyncStore
{
	private static GcSyncStore _instance = null;

	private static final long GCSYNC_PERSISTENT_STORE_KEY = 0x366b5697fe5512d1L;
	private static final long RUNTIME_STORE_KEY = 0x366b5697fe5512d1L;

	private static PersistentObject _store = null;
	private IntHashtable _data = null;
	private int _options =0;
	
	// do not change key values
	private static final int EMAIL_KEY = 0;
	private static final int AUTH_TOKEN_KEY = 1;
	private static final int PASSWORD_KEY = 2;
	private static final int OPTIONS_KEY = 3;
	private static final int LAST_SYNC_DATE_KEY = 4;

	private boolean contactListenerEnabled = true;

	static
	{
		if (RuntimeStore.getRuntimeStore().get(RUNTIME_STORE_KEY) == null)
			_instance = new GcSyncStore();

	}

	public static GcSyncStore instance()
	{
		return (GcSyncStore) RuntimeStore.getRuntimeStore().get(RUNTIME_STORE_KEY);
	}

	private GcSyncStore()
	{
		_store = PersistentStore.getPersistentObject(GCSYNC_PERSISTENT_STORE_KEY);

		try
		{
			_data = (IntHashtable) _store.getContents();

			if (_data == null)
			{
				_data = new IntHashtable();
			}
			else
				_options =( (Integer) _data.get(OPTIONS_KEY)).intValue();
		}
		catch (Throwable t)
		{
			System.out.println("some type of exception");
		}

		RuntimeStore.getRuntimeStore().put(RUNTIME_STORE_KEY, this);

		initDefaultOptions();
	}

	private void initDefaultOptions()
	{

		_options = _options | GcSyncConstants.OPTION_CONFLICT_GOOGLE;
		_options = _options | GcSyncConstants.OPTION_SYNC_MANUAL;
		
		this.commit();

	}

	public void storeLastSyncDate(String date)
	{
		if (date == null)
			date = "";

		store(LAST_SYNC_DATE_KEY, date);
	}

	public String getLastSyncDate()
	{
		return (String) _data.get(LAST_SYNC_DATE_KEY);

	}

	public void storeConflictOption(int optionMask)
	{
		// clear both options, then set the new value.
		_options &=~GcSyncConstants.OPTION_CONFLICT_BLACKBERRY;
		_options &=~GcSyncConstants.OPTION_CONFLICT_GOOGLE;		
		_options |=	optionMask;
	}

	private void log(String string)
    {
		System.out.println(string);
	    
    }

	public int getConflictOption()
	{
		int option = 0;
		if((_options&GcSyncConstants.OPTION_CONFLICT_BLACKBERRY)==GcSyncConstants.OPTION_CONFLICT_BLACKBERRY)
			option= GcSyncConstants.OPTION_CONFLICT_BLACKBERRY;
		else if((_options&GcSyncConstants.OPTION_CONFLICT_GOOGLE)==GcSyncConstants.OPTION_CONFLICT_GOOGLE)
			option= GcSyncConstants.OPTION_CONFLICT_GOOGLE;

		return option;

	}

	public void storeSyncOption(int optionMask)
	{

		_options &=~GcSyncConstants.OPTION_SYNC_AUTOMATIC;
		_options &=~GcSyncConstants.OPTION_SYNC_MANUAL;		
		_options |=	optionMask;
	}

	public int getSyncOption()
	{
		int option=0;
		if((_options&GcSyncConstants.OPTION_SYNC_AUTOMATIC)==GcSyncConstants.OPTION_SYNC_AUTOMATIC)
			option= GcSyncConstants.OPTION_SYNC_AUTOMATIC;
		else if((_options&GcSyncConstants.OPTION_SYNC_MANUAL)==GcSyncConstants.OPTION_SYNC_MANUAL)
			option= GcSyncConstants.OPTION_SYNC_MANUAL;
			

		return option;

	}

	public void storeEmail(String email)
	{
		store(EMAIL_KEY, email);
	}

	public String getEmail()
	{
		return (String) _data.get(EMAIL_KEY);
	}

	public void storeAuthToken(String auth)
	{
		store(AUTH_TOKEN_KEY, auth);
	}

	public String getAuthToken()
	{
		return (String) _data.get(AUTH_TOKEN_KEY);
	}

	public void storePassword(String password)
	{
		store(PASSWORD_KEY, CryptoUtil.encryptString(password));
	}

	public String getPassword()
	{
		return CryptoUtil.decryptString((String) _data.get(PASSWORD_KEY));
	}

	private void store(int key, Object value)
	{
		_data.put(key, value);
	}

	public void commit()
	{
		store(this.OPTIONS_KEY, new Integer(_options));
		_store.setContents(_data);
		_store.commit();
	}

	public boolean isContactListenerEnabled()
	{
		return contactListenerEnabled;
	}

	public void setContactListenerEnabled(boolean contactListenerEnabled)
	{
		this.contactListenerEnabled = contactListenerEnabled;
	}
}
